package lt.marsrutai.mobile.android.manager;

import lt.marsrutai.mobile.android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;

public class ActivityManager {

    private Activity managedActivity;
    private Animation downloadingBarAnimation;

    private int noDataMessageResId;

    public ActivityManager(Activity managedActivity) {
        this.managedActivity = managedActivity;
        this.noDataMessageResId = R.string.no_data_message;
    }
    
    public ActivityManager(Activity managedActivity, int noDataMessageResId) {
        this.managedActivity = managedActivity;
        this.noDataMessageResId = noDataMessageResId;
    }

    public final Runnable errorRes = new Runnable() {

        @Override
        public void run() {
            dismissDownloadLoadingBar();

            AlertDialog.Builder builder = new AlertDialog.Builder(managedActivity);
            builder.setIcon(android.R.drawable.ic_dialog_alert).setTitle(getString(R.string.no_data_title))
                    .setMessage(getString(getNoDataMessageResId())).setCancelable(false)
                    .setPositiveButton(getString(R.string.back), new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            managedActivity.finish();
                        }
                    });
            builder.create().show();
        }
    };

    public void runOnUiThread(Runnable action) {
        if (!managedActivity.isFinishing()) {
            managedActivity.runOnUiThread(action);
        }
    }

    private String getString(int resId) {
        return managedActivity.getString(resId);
    }

    public void showDownloadLoadingBar() {
        if (null == downloadingBarAnimation) {
            downloadingBarAnimation = AnimationUtils.loadAnimation(managedActivity, R.anim.rotate_360);
        }

        View progressSpinner = managedActivity.findViewById(R.id.progress_spinner);
        if (null != progressSpinner) {
            managedActivity.findViewById(R.id.spinner).startAnimation(downloadingBarAnimation);
            progressSpinner.setVisibility(View.VISIBLE);
        }
    }

    public void dismissDownloadLoadingBar() {
        View progressSpinner = managedActivity.findViewById(R.id.progress_spinner);
        if (null != progressSpinner) {
            managedActivity.findViewById(R.id.spinner).clearAnimation();
            progressSpinner.setVisibility(View.GONE);
        }
    }

    public void makeToast(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    public int getNoDataMessageResId() {
        return noDataMessageResId;
    }

    public void setNoDataMessageResId(int noDataMessageResId) {
        this.noDataMessageResId = noDataMessageResId;
    }

}
